package com.loie.datacenter.service.impl;

import com.loie.datacenter.model.bo.OriginUserBO;
import com.loie.datacenter.model.bo.ParamBO;
import com.loie.datacenter.model.bo.UserListBO;
import com.loie.datacenter.model.bo.UserOriginBO;
import com.loie.datacenter.service.UserListService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zhl
 * @create 2021/9/7 13:50
 */
@Transactional(readOnly = false)
@Service("userListService")
@AllArgsConstructor
public class UserListServiceImpl implements UserListService {

   final com.loie.datacenter.mapper.UserListMapper UserListMapper;
//    @Autowired
//    private  UserListMapper userListMapper;

    @Override
    public List<OriginUserBO> getOriginUserList(ParamBO bo) {
        return UserListMapper.getOriginUserList(bo);
    }

    @Override
    public void updateUserByUser(String password, String user) {

        UserListMapper.updateUserByUser(password, user);
    }

    @Override
    public
    UserListBO getUserInfo(UserListBO bo) {
        return UserListMapper.selectById(bo.getId());
    }

    @Override
    public
    UserListBO getUserInfoByUser(String user) {
        UserListBO userListBO = UserListMapper.getUserInfoByUser(user);
        if (userListBO != null) {
            List<String> list = UserListMapper.getCustomerIdByUserId(userListBO.getId());
            userListBO.setCustomerId(StringUtils.join(list, ","));

        }
        return userListBO;
    }


    @Override
    public List<UserListBO> getUserList(UserListBO bo) {

        List<UserListBO> userListBOList = UserListMapper.getUserList(bo);
        for (UserListBO userListBO : userListBOList) {
            List<String> menuList = UserListMapper.getCustomerIdByUserId(userListBO.getId());
            userListBO.setCustomerId(StringUtils.join(menuList, ","));
        }
        return userListBOList;
    }

    @Override
    public void updateUserList(UserListBO bo) {
        if (StringUtils.isNotBlank(bo.getId())) {
            UserListMapper.updateById(bo);
        } else {
            bo.setId(StringUtils.upperCase(UUID.randomUUID().toString().replace("-", "")));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            bo.setCreatedDate(date);
            UserListMapper.insert(bo);
        }
        String customerId = bo.getCustomerId();
        String[] list = customerId.split(",");
        UserListMapper.deleteUserOriginByUserId(bo.getId());
        for (String id : list) {
            UserOriginBO userOriginBO = new UserOriginBO();
            userOriginBO.setUserId(bo.getId());
            userOriginBO.setId(StringUtils.upperCase(UUID.randomUUID().toString().replace("-", "")));
            userOriginBO.setCustomerId(id);
            UserListMapper.insertUserOrigin(userOriginBO);
        }
    }

    @Override
    public void deleteUserList(UserListBO bo) {
        UserListMapper.deleteById(bo.getId());
        UserListMapper.deleteUserOriginByUserId(bo.getId());
    }

    @Override
    public boolean isExist(UserListBO bo) {
        boolean flag = false;
        long count = UserListMapper.isExist(bo);
        if (count > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public void setIsLocked(String id, String isLocked) {
        UserListMapper.setIsLocked(id, isLocked);
    }

    @Override
    public void updatePassword(String id, String newPassword) {
        UserListMapper.updatePassword(id, newPassword);
    }

    @Override
    public int getUserListCount(UserListBO bo) {
        return UserListMapper.getUserListCount(bo);
    }

    @Override
    public int getOriginUserListCount(ParamBO bo) {
        return UserListMapper.getOriginUserListCount(bo);
    }
}
